package com.ttxs.uaa.api;

import com.ttxs.common.core.properties.TokenProperties;
import com.ttxs.common.core.vo.LoginUser;
import com.ttxs.common.core.utils.AuthUtils;
import com.ttxs.common.core.constant.Common;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

public abstract class BaseApi {
    /**
     * 此处是线程安全的
     */
    @Resource
    private HttpServletRequest httpServletRequest;

    @Autowired
    private TokenProperties tokenProperties;

    /**
     * 获取请求头中传递的platformId
     * @return
     */
    protected String getPlatformId() {
        return (String) getLoginUser().getExtra().get(Common.HttpHeaders.PLATFORM_ID);
    }

    protected String getPlatformName() {
        return (String) getLoginUser().getExtra().get(Common.HttpHeaders.PLATFORM_NAME);
    }

    protected Long getUserId() {
        return getLoginUser().getUserId();
    }

    protected String getToken() {
        return httpServletRequest.getHeader(tokenProperties.getName());
    }

    protected LoginUser getLoginUser() {
        return AuthUtils.getLoginUser(getToken());
    }
}
